#include "miniutil/logservice.h"
#include "miniutil/thread.h"
#include "miniutil/threadpool.h"
#include "minitest/testcase.h"
#include "miniutil/tinyxml.h"
#include "miniutil/strutil.h"
#include "miniutil/socketcmn.h"

#ifdef WIN32
#include "windows.h"
#endif


class logservice_case : public testcase
{
public:
    logservice_case()
    {
        miniutil::frame::LogService::InitService();
    }

    ~logservice_case()
    {
    }

    int entryfunc()
    {
        //FAUTOENTRY;
        FENTRY;
        FRETURN(0);
    }

    void test_consolelogger()
    {
        miniutil::frame::galoggservice.Start();
        const char* configxml = 
            "<?xml version='1.0'?>"
            "<loggers>"
                "<logger name='consolelogger' >"
                    "<config level='debug|crit|trace'></config>"
                "</logger>"
            "</loggers>";


        miniutil::tinyxml::TiXmlDocument tdoc;
        tdoc.Parse(configxml);
        
        miniutil::frame::galoggservice.config(tdoc.RootElement());
        miniutil::frame::galoggservice.log(miniutil::frame::Logger::LOG_LEVEL_DEBUG, "aaa\n");
        miniutil::frame::galoggservice.log(miniutil::frame::Logger::LOG_LEVEL_DEBUG, "bbb\n");
        F_DEBUG("fdebug\n");
        
        entryfunc();
        miniutil::thread::sleep(100);
        miniutil::frame::galoggservice.Stop();

		return;
    }

    void test_taglogger()
    {
        miniutil::frame::galoggservice.Start();
        const char* configxml = 
            "<?xml version='1.0'?>"
            "<loggers>"
                "<logger name='tagfilelogger' >"
                    "<config>"
                        "<consolelogger tag='haha' printtag='false'>"
                            "<config  append='time'></config>"
                        "</consolelogger>"
                        "<consolelogger tag='xixi|keke'>"
                            "<config></config>"
                        "</consolelogger>"
                    "</config>"
                "</logger>"
            "</loggers>";

        miniutil::tinyxml::TiXmlDocument tdoc;
        tdoc.Parse(configxml);
        
        miniutil::frame::galoggservice.config(tdoc.RootElement());
        T_LOG("haha", "%s%d\n", "aaa", 1);
        T_LOG("keke", "%s%d\n", "what is your name?", 2);
        miniutil::thread::sleep(100);
        miniutil::frame::galoggservice.Stop();
        std::cout<<"log fin"<<std::endl;

    }

    static void* logserfunc(void* pvoid)
    {
        while(true)
        {
            std::string ips = miniutil::socketcmn::ip_i2s(12345678, 1111);
            T_LOG("p2pdebug", "%s%d %s\n", "what is your name?", 2, ips.c_str());
            miniutil::thread::sleep(10);
        }
        return NULL;
    }

    void testmultithreadlog()
    {
        const char* configxml = "<?xml version='1.0'?>"
            "<loggers>"
            "<logger name='autotagfilelogger'>"
            "<config exclude='p2pdebug' savedir='vlog'  usepid='true' usetime='true' append='threadid|time'/>"
            "</logger>"
            "<logger name='tagfilelogger'>"
            "<config append='time'>"
            "<consolelogger tag='console' printtag='false'>"
            "<config></config>"
            "</consolelogger>"
            "<filelogger tag='p2pdebug' printtag='false'>"
            "<config savedir='vlog' usepid='true' append='threadid|time'></config>"
            "</filelogger>"
            "</config>"               	                
            "</logger>"	        
            "</loggers>";

        miniutil::tinyxml::TiXmlDocument tdoc;
        tdoc.Parse(configxml);
        miniutil::frame::galoggservice.config(tdoc.RootElement());
        miniutil::frame::galoggservice.Start();
        //create many thread..
        for(unsigned int i=0; i< 10; i++)
        {
	        DWORD dwThreadID;
	        CreateThread( (LPSECURITY_ATTRIBUTES) NULL, /*security attribute*/
				0,/* size of thread stack, same as main thread by default*/
				(LPTHREAD_START_ROUTINE)logserfunc, /* thread entry function */
				this, 0, &dwThreadID);
        }

        while(true)
        {
            miniutil::thread::sleep(100);
        }
    }

	virtual void test()
	{
        //test_consolelogger();
        //test_taglogger();
        testmultithreadlog();
	}
};

testcase * t_logservice_1  = new logservice_case() ;